using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers;

/// <summary>
/// 站点统计控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class SiteStatisticController : ControllerBase
{
    private readonly ISiteStatisticService _siteStatisticService;

    public SiteStatisticController(ISiteStatisticService siteStatisticService)
    {
        _siteStatisticService = siteStatisticService;
    }

    /// <summary>
    /// 获取所有统计数据
    /// </summary>
    [HttpGet]
    public async Task<ActionResult<IEnumerable<SiteStatistic>>> GetAll()
    {
        var statistics = await _siteStatisticService.GetAllAsync();
        return Ok(statistics);
    }

    /// <summary>
    /// 根据ID获取统计数据
    /// </summary>
    [HttpGet("{id}")]
    public async Task<ActionResult<SiteStatistic>> GetById(Guid id)
    {
        var statistic = await _siteStatisticService.GetByIdAsync(id);
        if (statistic == null)
        {
            return NotFound();
        }
        return Ok(statistic);
    }

    /// <summary>
    /// 根据日期获取统计数据
    /// </summary>
    [HttpGet("date/{date}")]
    public async Task<ActionResult<SiteStatistic>> GetByDate(DateOnly date)
    {
        var statistic = await _siteStatisticService.GetByDateAsync(date);
        if (statistic == null)
        {
            return NotFound();
        }
        return Ok(statistic);
    }

    /// <summary>
    /// 获取日期范围内的统计数据
    /// </summary>
    [HttpGet("range")]
    public async Task<ActionResult<IEnumerable<SiteStatistic>>> GetByDateRange(
        [FromQuery] DateOnly startDate,
        [FromQuery] DateOnly endDate)
    {
        var statistics = await _siteStatisticService.GetByDateRangeAsync(startDate, endDate);
        return Ok(statistics);
    }

    /// <summary>
    /// 创建统计数据
    /// </summary>
    [HttpPost]
    public async Task<ActionResult<SiteStatistic>> Create(SiteStatistic siteStatistic)
    {
        var createdStat = await _siteStatisticService.CreateAsync(siteStatistic);
        return CreatedAtAction(nameof(GetById), new { id = createdStat.StatId }, createdStat);
    }

    /// <summary>
    /// 更新统计数据
    /// </summary>
    [HttpPut("{id}")]
    public async Task<IActionResult> Update(Guid id, SiteStatistic siteStatistic)
    {
        if (id != siteStatistic.StatId)
        {
            return BadRequest();
        }

        await _siteStatisticService.UpdateAsync(siteStatistic);
        return NoContent();
    }

    /// <summary>
    /// 删除统计数据
    /// </summary>
    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        await _siteStatisticService.DeleteAsync(id);
        return NoContent();
    }

    /// <summary>
    /// 更新今日统计数据
    /// </summary>
    [HttpPost("update-today")]
    public async Task<ActionResult<SiteStatistic>> UpdateTodayStats()
    {
        var stats = await _siteStatisticService.UpdateTodayStatsAsync();
        return Ok(stats);
    }
} 